import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_utils/flutter_utils.dart';

void main() {
  group('retryAsync', () {
    test('retryAsync simple', () async {
      final result = await retryAsync<bool>(
          action: (retryCount) => Future.value(true),
          maxRetry: 2,
          retryIf: (_) => true,
          interval: (_) => const Duration(seconds: 1));
      expect(result, true);
    });
    test('retryAsync always fail', () async {
      try {
        await retryAsync<bool>(
          action: (retryCount) => throw RangeError('message'),
          maxRetry: 2,
          retryIf: (e) => e is RangeError,
          interval: (_) => const Duration(milliseconds: 500),
        );
      } catch (e) {
        expect(e is RangeError, true);
      }
    });
    test('retryAsync retry suc', () async {
      final result = await retryAsync<bool>(
        action: (retryCount) async {
          if (retryCount == 3) {
            return true;
          } else {
            throw 'fail';
          }
        },
        retryIf: (e) {
          return true;
        },
        maxRetry: 3,
      );
      expect(result, true);
    });
  });
}
